﻿<template>
    <div class="esBusiness-container">
        <el-card shadow="hover">
            <el-form :model="state.queryParams" ref="queryForm" :inline="true">
                <el-form-item :label-width='70' label="业务名称" prop="name">
                    <el-input placeholder="业务名称" clearable class='ml10' @keyup.enter="handleQuery"
                              v-model="state.queryParams.name"/>
                </el-form-item>
                <el-form-item>
                    <el-button-group>
                        <el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'esBusiness:query'" :loading="state.loading">
                            查询
                        </el-button>
                        <el-button icon="ele-Refresh" @click="resetQuery" v-auth="'esBusiness:query'"> 重置</el-button>
                    </el-button-group>
                </el-form-item>
            </el-form>
        </el-card>

        <el-card shadow="hover" style="margin-top: 8px">
            <div style="margin: -8px 0 10px 0;">
                <el-button type="success" plain icon="ele-Plus" @click="openAddEsBusiness"
                           v-auth="'esBusiness:add'">
                    新增
                </el-button>
            </div>
            <el-table
                :data="state.data"
                style="width: 100%"
                v-loading="state.loading"
                tooltip-effect="light"
                row-key="id"
                border stripe highlight-current-row>
                <el-table-column header-align="center" type="index" label="序号" width="55" align="center"/>
                <el-table-column header-align="center" prop="name" label="业务名称" show-overflow-tooltip/>
                <el-table-column header-align="center" prop="alias" label="业务简称" show-overflow-tooltip/>
                <el-table-column header-align="center" prop="code" label="业务编码" show-overflow-tooltip/>
                <el-table-column header-align="center" prop="remark" label="备注" show-overflow-tooltip/>
                <el-table-column header-align="center" prop="createTime" label="创建时间" width="160" align="center"
                                 show-overflow-tooltip/>
                <el-table-column label="操作" width="90" align="center" fixed="right" show-overflow-tooltip>
                    <template #default="scope">
                        <el-button size="small" text type="primary"
                                   @click="openEditEsBusiness(scope.row)" v-auth="'esBusiness:update'"> 编辑
                        </el-button>
                        <el-button size="small" text type="danger" @click="delEsBusiness(scope.row)"
                                   v-auth="'esBusiness:delete'"> 删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
                           :page-sizes="[20, 50, 100]" v-model:current-page="state.queryParams.pageNum" background
                           v-model:page-size="state.queryParams.pageSize"
                           layout="total, sizes, prev, pager, next, jumper"
                           :total="state.total"/>
            <EditEsBusiness ref="editEsBusinessRef" :title="state.editTitle"/>
        </el-card>
    </div>
</template>

<script lang="ts" setup name="esBusiness">

import {onMounted, onUnmounted, reactive, ref} from 'vue';
import {ElMessageBox, ElMessage} from 'element-plus';
import mittBus from '/@/utils/mitt';
import EditEsBusiness from '/@/views/exocytosis/operate/business/component/editEsBusiness.vue';

import {EsBusiness} from '/@/api/models';
import SysEsBusinessApi from '/@/api/exocytosis/business';

// 定义变量内容
const editEsBusinessRef = ref<InstanceType<typeof EditEsBusiness>>();
const state = reactive({
    loading: false,
    data: [] as Array<EsBusiness>,
    total: 0 as any,
    queryParams: {
        name: undefined,
        pageNum: 1,
        pageSize: 20,
    },
    editTitle: '',
});

onMounted(async () => {
    mittBus.on('submitRefresh', () => {
        handleQuery();
    });
});

onUnmounted(() => {
    mittBus.off('submitRefresh');
});

// 查询操作
const handleQuery = async () => {
    state.loading = true;
    const res = await SysEsBusinessApi().queryRecord(state.queryParams);
    state.data = res.data.result.items ?? [];
    state.total = res.data.result?.total;
    state.loading = false;
};

// 重置操作
const resetQuery = () => {
    // 1. 批量重置为undefined
    const keepKeys: string[] = ['pageNum', 'pageSize',]; // 声明需要保留的字段
    Object.keys(state.queryParams).forEach(key => {
        if (!keepKeys.includes(key))
            state.queryParams[key] = undefined;
    });
    // 2. 数据
    state.data = [];
};

// 打开新增页面
const openAddEsBusiness = () => {
    state.editTitle = '添加';
    editEsBusinessRef.value?.openDialog({status: 1});
};

// 打开编辑页面
const openEditEsBusiness = (row: EsBusiness) => {
    state.editTitle = '编辑';
    editEsBusinessRef.value?.openDialog(row);
};

// 删除
const delEsBusiness = (row: any) => {
    ElMessageBox.confirm(`确定删除：【${row.name}】?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    })
        .then(async () => {
            let res = await SysEsBusinessApi().deletePost({id: row.id});
            if (res.data.resCode != '0000')
                ElMessage.error(res.data.resMessage);
            else
                ElMessage.success("删除成功");
            await handleQuery();
        })
        .catch(() => {
        });
};

// 改变页面容量
const onHandleSizeChange = (val: number) => {
    state.queryParams.pageSize = val;
    handleQuery();
};

// 改变页码序号
const onHandleCurrentChange = (val: number) => {
    state.queryParams.pageNum = val;
    handleQuery();
};

</script>
